#include "binary_tree.h"

#include <iostream>
using std::cin;

#include <vector>
using std::vector;

void solution()
{
	int n = 0;
	cin >> n;
	vector<node<int>> v(n+1);
	int left = 0;
	int right = 0;
	for (int i = 1; i <= n; ++i)
	{
		v[i].m_val = i;
		cin >> left >> right;
		if (left != -1)
		{
			v[i].m_left = &v[left];
		}
		if (right != -1)
		{
			v[i].m_right = &v[right];
		}
		
	}

	binary_tree<int> b(&v[1]);

	vector<int> pre;
	b.preorder(b.getroot(), pre);
	for (auto e : pre)
	{
		cout << e << " ";
	}
	cout << endl;

	vector<int> in;
	b.inorder(b.getroot(), in);
	for (auto e : in)
	{
		cout << e << " ";
	}
	cout << endl;
	
	vector<int> post;
	b.postorder(b.getroot(), post);
	for (auto e : post)
	{
		cout << e << " ";
	}
	cout << endl;

	vector<int> level;
	b.levelorder(b.getroot(), level);
	for (auto e : level)
	{
		cout << e << " ";
	}
	cout << endl;

	for (int i = 1; i <= n; ++i)
	{
		cout << b.size(&v[i]) << " ";
	}
	cout << endl;

	for (int i = 1; i <= n; ++i)
	{
		cout << b.hight(&v[i]) << " ";
	}
	cout << endl;

}

void solution2()
{
	int n = 0;
	cin >> n;
	vector<int> pre;
	int tmp = 0;
	for (int i = 0; i < n; ++i)
	{
		cin >> tmp;
		pre.push_back(tmp);
	}

	vector<int> in;
	for (int i = 0; i < n; ++i)
	{
		cin >> tmp;
		in.push_back(tmp);
	}

	binary_tree<int> t;
	node<int>* root = t.build_by_preorder_and_inorder(pre, 0, pre.size(), in, 0, in.size());
	vector<int> post;
	t.postorder(root, post);
	for (auto e : post)
	{
		cout << e << " ";
	}
	cout << endl;

}

int main()
{
	//solution();

	solution2();

	return 0;
}